unit dropitemselection;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin,
  ButtonPanel, database;

type

  { TDropItemSelectionForm }

  TDropItemSelectionForm = class(TForm)
    ResultButtonPanel: TButtonPanel;
    ArmourComboBox: TComboBox;
    WeaponComboBox: TComboBox;
    ItemComboBox: TComboBox;
    DropItemGroupBox: TGroupBox;
    OneDivLabel: TLabel;
    ProbabilityGroupBox: TGroupBox;
    NoneRadioButton: TRadioButton;
    ItemRadioButton: TRadioButton;
    WeaponRadioButton: TRadioButton;
    ArmourRadioButton: TRadioButton;
    DenominatorSpinEdit: TSpinEdit;
    procedure ArmourRadioButtonChange(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure DenominatorSpinEditChange(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ItemRadioButtonChange(Sender: TObject);
    procedure NoneRadioButtonChange(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure WeaponRadioButtonChange(Sender: TObject);
  private

  public
    SelectedKind: Integer;
    SelectedDataId: Integer;
    SelectedDenominator: Integer;
    function ShowSelection(Kind, DataId, Denominator: Integer; Db: TDatabase): Boolean;
  end;

var
  DropItemSelectionForm: TDropItemSelectionForm;

implementation

uses
  Math, constants, comboboxhelper;

{$R *.lfm}

{ TDropItemSelectionForm }

procedure TDropItemSelectionForm.FormShow(Sender: TObject);
var
  RadioItemWidth: Integer;
begin
  ItemRadioButton.AutoSize := True;
  WeaponRadioButton.AutoSize := True;
  ArmourRadioButton.AutoSize := True;

  RadioItemWidth := Max(ItemRadioButton.Width,
                 Max(WeaponRadioButton.Width, ArmourRadioButton.Width));

  ItemRadioButton.AutoSize := False;
  WeaponRadioButton.AutoSize := False;
  ArmourRadioButton.AutoSize := False;

  ItemRadioButton.Width := RadioItemWidth;
  WeaponRadioButton.Width := RadioItemWidth;
  ArmourRadioButton.Width := RadioItemWidth;
end;

procedure TDropItemSelectionForm.ArmourRadioButtonChange(Sender: TObject);
begin
  if ArmourRadioButton.Checked then
    SelectedKind := DROP_ITEM_KIND_ARMOUR;
   ArmourComboBox.Enabled := ArmourRadioButton.Checked;
   DenominatorSpinEdit.Enabled := not NoneRadioButton.Checked;
end;

procedure TDropItemSelectionForm.CancelButtonClick(Sender: TObject);
begin
  ModalResult := mrCancel;
  Close;
end;

procedure TDropItemSelectionForm.DenominatorSpinEditChange(Sender: TObject);
begin
  SelectedDenominator := DenominatorSpinEdit.Value;
end;

procedure TDropItemSelectionForm.FormHide(Sender: TObject);
begin
  ItemComboBox.Items.Clear;
  WeaponComboBox.Items.Clear;
  ArmourComboBox.Items.Clear
end;

procedure TDropItemSelectionForm.ItemRadioButtonChange(Sender: TObject);
begin
  if ItemRadioButton.Checked then
    SelectedKind := DROP_ITEM_KIND_ITEM;
  ItemComboBox.Enabled := ItemRadioButton.Checked;
  DenominatorSpinEdit.Enabled := not NoneRadioButton.Checked;
end;

procedure TDropItemSelectionForm.NoneRadioButtonChange(Sender: TObject);
begin
  if NoneRadioButton.Checked then
    SelectedKind := DROP_ITEM_KIND_NONE;
   DenominatorSpinEdit.Enabled := not NoneRadioButton.Checked;
end;

procedure TDropItemSelectionForm.OKButtonClick(Sender: TObject);
begin
  ModalResult := mrOk;
  Close;
end;

procedure TDropItemSelectionForm.WeaponRadioButtonChange(Sender: TObject);
begin
  if WeaponRadioButton.Checked then
    SelectedKind := DROP_ITEM_KIND_WEAPON;
   WeaponComboBox.Enabled := WeaponRadioButton.Checked;
   DenominatorSpinEdit.Enabled := not NoneRadioButton.Checked;
end;

function TDropItemSelectionForm.ShowSelection(Kind, DataId,
  Denominator: Integer; Db: TDatabase): Boolean;
begin
  ModalResult := mrCancel;

  SelectedKind := Kind;
  SelectedDataId := DataId;
  SelectedDenominator := Denominator;

  FillDbArrayComboBox(ItemComboBox, Db.Items);
  FillDbArrayComboBox(WeaponComboBox, Db.Weapons);
  FillDbArrayComboBox(ArmourComboBox, Db.Armours);

  case Kind of
    DROP_ITEM_KIND_NONE:
      NoneRadioButton.Checked := True;
    DROP_ITEM_KIND_ITEM: begin
        ItemRadioButton.Checked := True;
        ItemComboBox.ItemIndex := DataId;
      end;
    DROP_ITEM_KIND_WEAPON: begin
        WeaponRadioButton.Checked := True;
        WeaponComboBox.ItemIndex := DataId;
      end;
    DROP_ITEM_KIND_ARMOUR: begin
        ArmourRadioButton.Checked := True;
        ArmourComboBox.ItemIndex := DataId;
      end;
  end;
  DenominatorSpinEdit.Value := Denominator;

  ShowModal;

  ShowSelection := ModalResult = mrOK;
end;

end.

